package cn.csxw.openchina.dagger2.presenter;

import java.util.ArrayList;

import javax.inject.Inject;

import cn.csxw.openchina.entity.bean.BlogBean;
import cn.csxw.openchina.entity.bean.OsChinaBean;
import cn.csxw.openchina.ui.fragment.syntheticalFragment.RecommendFragment;
import retrofit2.Call;

/**
 * Created by hp on 2017/1/15.
 */

public class RecommendFragmentPresenter extends BasePresenter {
    public RecommendFragment mRecommendFragment;

    @Inject
    public RecommendFragmentPresenter(RecommendFragment recommendFragment) {
        mRecommendFragment = recommendFragment;
    }

    @Override
    public void processData(OsChinaBean data) {
        if (data != null) {
            if (!hasMoreData(data)) {
                mRecommendFragment.noMoreData();
                mRecommendFragment.mSrfLayout.setRefreshing(false);
            } else {
                ArrayList<BlogBean> blogs = (ArrayList<BlogBean>) data.blogs;
                if (mRecommendFragment.mRecommendAdapter.getData().size() != 0) {
                    mRecommendFragment.mRecommendAdapter.addData(blogs);
                } else {
                    mRecommendFragment.mRecommendAdapter.setData(blogs);
                }
                mRecommendFragment.loadMoreCompleted();
                mRecommendFragment.mSrfLayout.setRefreshing(false);
            }
        }
    }

    @Override
    public void callNetWork(Object... params) {
        //todo 自己捕获异常,
        if (params.length != 3 || !(params[0] instanceof Integer)
                || !(params[1] instanceof Integer) || !(params[2] instanceof String)) {
            throw new IllegalArgumentException("参数必需是int , int ,String,三个类型");
        }
        Call<OsChinaBean> recommendfInfo = mOsChinaServer.getRecommendfInfo((int) params[0], (int) params[1], (String) params[2]);
        recommendfInfo.enqueue(mCallback);
    }
}
